﻿package net.wangit.view.element.component;

import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.support.AbstractElementWriter;
import net.wangit.util.Checker;
import net.wangit.util.StringUtil;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.util.ElementUtil;
import net.wangit.view.util.Html;

public class EditorElementWriter extends AbstractElementWriter {

	private static final String EDITOR_ROOT = "/common/fckeditor/";

	public EditorElementWriter() {
	}

	protected void writeText(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		Html.set(html, ElementUtil.getValueOrDefault(ac, ec, row));
	}

	protected void writeTextWithValue(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeText(ac, html, ec, row);
	}

	protected void writeControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControl(ac, html, ec, row, true);
	}

	protected void writeReadOnlyControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControl(ac, html, ec, row);
	}

	protected void writeDisabledControl(ActionContext actioncontext,
			StringBuffer stringbuffer, ElementComponent elementcomponent,
			Row row1) {
	}

	protected void writeInvisibleControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControl(ac, html, ec, row, false);
	}

	protected void writeHidden(ActionContext actioncontext,
			StringBuffer stringbuffer, ElementComponent elementcomponent,
			Row row1) {
	}

	private void writeControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row, boolean visible) {
		html.append("<span");
		Html.setStyleWithWidthAndHeight(html, ec.getWidth(), ec.getHeight());
		if (!visible)
			html.append(" style=\"display:none\"");
		html.append(">");
		String name = ElementUtil.getAlias(ec);
		String varName = (new StringBuilder("_fck_")).append(
				StringUtil.replace(name, ".", "_")).toString();
		html.append("<script type=\"text/javascript\">");
		html.append("var ").append(varName).append(" = new FCKeditor('")
				.append(name).append("');");
		html.append(varName).append(".BasePath='").append(
				EditorElementWriter.EDITOR_ROOT).append("';");
		html.append(varName).append(".Height='100%';");
		String toolbarSet = ElementUtil.resolveCustom(ac, ec, row);
		if (!Checker.isEmpty(toolbarSet))
			html.append(varName).append(".ToolbarSet='").append(toolbarSet)
					.append("';");
		String value = ElementUtil.getValueOrDefault(ac, ec, row);
		if (value != null) {
			value = value.replaceAll("\r\n", "");
			value = value.replaceAll("\r", "");
			value = value.replaceAll("\n", "");
			value = value.replaceAll("\"", "'");
			html.append(varName).append(".Value=\"").append(value)
					.append("\";");
		}
		html.append(varName).append(".Create();");
		html.append("</script>");
		html.append("</span>");
	}
}
